﻿/**
 *	html/pem_data_device.js
 *
 *	(C) 2014 Yuanjj
 *
 * 	history:
 * 		2014-07-30		create the file
 */

/**
 *	init device
 */
function init_device()
{
    _create_device_type();
    _create_device();
    _create_device_values();
    _create_device_status();
}

/**
 *	create device type store
 */
var g_device_type;
function _create_device_type()
{
    Ext.define
    ( "model_device_type", {
         extend:"Ext.data.Model",
         fields:[
             {name:"serial",		type:"int"},		// serial
             {name:"type",			type:"string"},		// type id (unique)
             {name:"name",			type:"string"},		// type name ("温度")
             {name:"probe",			type:"string"},		// probe name ("温度传感器")
             {name:"img",			type:"string"},		// device img path
             {name:"append",		type:"string"}		// append
         ],
         proxy:"memory"
    });

    g_device_type = new Ext.data.ArrayStore
    ({
         autoLoad:true,
         model:"model_device_type",
         /*
         data:[
             [0, "sf6", "sf6", "SF6", "./res/f150.jpg", ""],
         ]
         */
    });
}

/**
 *	initialize the device data
 */
var g_device;
function _create_device()
{
    Ext.define
    ("model_data_device", {
         extend:"Ext.data.Model",
         fields:[
             {name:"dev_id",		type:"string"},			// device function point( primary key )
             {name:"dev_type",		type:"string"},			// the device type (it is foreign key "type" in "g_device_type")
             {name:"dev_name",		type:"string"},			// device name("温度计")
             {name:"dev_pos",		type:"string"},			// the local address at the scene
             {name:"add_time",		type:"string"},			// device adding date
             {name:"machine",		type:"string"},			// the machine type
             {name:"tunnel_type",	type:"string"},			// the tunnel type
             {name:"tunnel_num",	type:"string"},			// the tunnel num
             {name:"pto_name",		type:"string"},			// the protocal
             {name:"pto_file",		type:"string"},			// the protocal file
             {name:"host",			type:"string"},			// the host id ( it is foreign key "host" in "g_host" )
             {name:"value",			type:"string"},			// the device last value
             {name:"x",				type:"int"},			// the device x coordinate
             {name:"y",				type:"int"},			// the device y coordinate
             {name:"setup",			type:"int"},			// setup flag. 0:not 1:setting 2:done
         ],
         proxy:"memory"
    });

    g_device = new Ext.data.ArrayStore
    ({
         autoLoad:true,
         model:"model_data_device",
    });
}

/**
 *	initialize the device values
 */
var g_device_values;
function _create_device_values()
{
    Ext.define
    ( "model_device_values", {
         extend:"Ext.data.Model",
         fields:[
             {name:"datetime", 		type:"string"},
             {name:"value", 		type:"float"},
         ],
         proxy:"memory"
    });

    g_device_values = new Ext.data.ArrayStore
    ({
         autoLoad:true,
         model:"model_device_values",
         data:[
             ["0", 14.6], ["2", 13.7], ["4", 12.9], ["6", 12.8], ["8", 19.1], ["10", 22.6],
             ["12", 23.5], ["14", 25.8], ["16", 25.3], ["18", 21.2], ["20", 18.8], ["22", 16.4],
         ]
    })
}

/**
 *	initialize the device status
 */
var g_device_status;
function _create_device_status()
{
    g_device_status = new Array();

    var dev_temp = new Array();

    dev_temp["status"] = 1;
    dev_temp[0] = "./res/f150.jpg";
    dev_temp[1] = "./res/s10.jpg";
    g_device_status["light"] = dev_temp;
}

/**
 *	update device record into db
 *
 *	@rec			[in] the device record
 */
function update_device_record( rec )
{
    if ( rec.isModified( "pto_name") )
    {
        var pto_record = g_protocal.findRecord( "pto_name", rec.get( "pto_name" ) );
        if ( null === pto_record )
            return;

        rec.set( "pto_file", pto_record.get( "pto_file" ) );
        rec.set( "setup", 1 );
    }

    var	dev_id 		= rec.get("dev_id");
    var	dev_type	= rec.get("dev_type");
    var	dev_name	= rec.get("dev_name");
    var	dev_pos		= rec.get("dev_pos");
    var	machine		= rec.get("machine");
    var	tunnel_num	= rec.get("tunnel_num");
    var	pto_name	= rec.get("pto_name");
    var	pto_file	= rec.get("pto_file");
    var	host		= rec.get("host");
    var	value		= rec.get("value");
    var	x			= rec.get("x");
    var	y			= rec.get("y");
    var	setup		= rec.get("setup");
    var param 		= dev_id + "," + dev_type + "," + dev_name + "," + dev_pos + "," + machine + "," +
                        tunnel_num + "," + pto_name + "," + pto_file + "," + host + "," + value + "," +
                        x + "," + y + "," + setup;

    qt_html.update_device( param );
}

/**
 *	callback for setting device type
 */
function callback_set_device_type( serial, type, name, probe, img, append )
{
    var index			= g_device_type.count();

    g_device_type.insert( index, [[serial, type, name, probe, img, append]] );
}

/**
 *	callback for setting device
 */
function callback_set_device( dev_id, dev_type, dev_name, dev_pos, add_time, machine,
                                tunnel_num, pto_name, host, value, x, y, setup )
{
    var index				= g_device.count();
    var tunnelrecord		= g_tunnel.findRecord( "number", tunnel_num );
    var protocalrecord		= g_protocal.findRecord( "pto_name", pto_name );

    if ( null === tunnelrecord || null === protocalrecord )
        return;

    var	tunnel_type			= tunnelrecord.get( "type" );
    var pto_file			= protocalrecord.get( "pto_file" );

    g_device.insert( index, [[dev_id, dev_type, dev_name, dev_pos, add_time, machine,
                              tunnel_type, tunnel_num, pto_name, pto_file, host, value, x, y, setup]] );
}

/**
 *	callback for setting device values
 */
function callback_set_device_values( step, values )
{
    var index = g_device_values.count();

    g_device_values.insert( index, [[step, values]] );
}
